package com.hyper_yang._05_juc.chap02;

/**
 * 演示死锁
 */
public class DeadlockDemo {

    private static final Object object1 = new Object();
    private static final Object object2 = new Object();

    public static void main(String[] args) {
        new Thread(()->{
            synchronized (object1){
                System.out.println("A 获取了 object1 对象锁 ");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("A 准备获取 object2 对象锁 ");
                synchronized (object2){
                    System.out.println("A 获取了 object2 对象锁 ");
                }
            }
        },"A").start();

        new Thread(()->{
            synchronized (object2){
                System.out.println("B 获取了 object2 对象锁 ");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("B 准备获取 object1 对象锁 ");
                synchronized (object1){
                    System.out.println("B 获取了 object1 对象锁 ");
                }
            }
        },"B").start();
    }
}